package kaizone.haining.chaobohui.json;

import org.json.JSONObject;

public class JSONResponse {
    public static final String CODE = "code";
    public static final String DATA = "data";
    public static final String DETAIL = "detail";
    public static final String EXTRADATA = "extraData";
    public static final String SUCCESS = "success";
    
    public static final String CODE_ILL_ARGS = "ill_args";
    
    public String code;
    public String detail;
    public String extraData;
    public boolean success;
    
    public static JSONResponse parse(JSONObject jsonObject){
        JSONResponse response = new JSONResponse();
        response.code = jsonObject.optString(JSONResponse.CODE);
        response.detail = jsonObject.optString(JSONResponse.DETAIL);
        response.success = jsonObject.optBoolean(JSONResponse.SUCCESS);
        return response;
    }
    
    public static JSONResponse parseJsonString(String jsonstring){
        JSONObject jsonObject = null;
        JSONResponse response = new JSONResponse();
        try{
            jsonObject = new JSONObject(jsonstring);            
            response.code = jsonObject.optString(JSONResponse.CODE);
            response.detail = jsonObject.optString(JSONResponse.DETAIL);
            response.success = jsonObject.optBoolean(JSONResponse.SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
        }
        return response;
    }
    
    public void setJSONResponse(JSONResponse response){
        code = response.code;
        detail = response.detail;
        success = response.success;
    }
    
    public JSONResponse getJsonResponse(){
        return this;
    }
    
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("code").append(":").append(code).append("\n");
        sb.append("detail").append(":").append(detail).append("\n");
        sb.append("extraData").append(":").append(extraData).append("\n");
        sb.append("success").append(":").append(success).append("\n");
        return sb.toString();
    }
    
}
